// pages/store/store.js
var app = getApp();
var req = require("../../utils/req.js");
var url = require("../../utils/url.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    proTabs: ['营业中', '未营业'],
    activeIndex: 0,
    current_page: 0,
    storeInfo: [],
    address:'',
    latitude: null,
    longitude: null,
    distance:null,
    value:"",
    storeAdress:[]
  },
  /**
     * 搜索框的内容
     */
  getInput:function(e){
    var value = e.detail.value;
    this.setData({
      value:value
    })
  },
  /**
     * 搜索
     */
  toSearch:function(){
    var that = this;
    var value = this.data.value;
      that.setData({
        current_page: 0,
      })
      that.data.storeInfo=[];
      that.getStoreList();
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
    this.getStoreList();
  },
  /**
   * 得到商家列表的数据
   */
  getStoreList: function () {
    var that = this;
    var value = that.data.value;
    var data;
    var location = app.globalData.location;
    data = {
      page: that.data.current_page,
      search:value
    }

    req.userRequest("请稍等", 'POST', url.user['queryStore'], data, function back(res) {
      console.log(res)
      var data = res.data;
      for (var i = 0; i < res.data.length; i++) {
        console.log(res.data[i].address.split('市')[1])
        res.data[i].address = res.data[i].address.split('省')[1]
      }

      if (that.data.storeInfo.length==0&&data.length==0){
        wx.showToast({
          icon: 'none',
          title: '暂无商家',
        })
      }

      //判断数据是否为null
      if (data.length!=0) {
        //合并数组
        that.data.storeInfo.push.apply(that.data.storeInfo, data);
        that.setData({
          storeInfo: that.data.storeInfo,
        })
        // that.data.storeInfo.address
        // console.log(that.data.storeInfo.address);
      }else{
        wx.showToast({
          icon:'none',
          title: '已经到底了！',
        })
      }
    });
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    console.log('fresh');
    var that = this;
    this.getProList(() => {
      that.data.isLoadedAll = false;
      wx.stopPullDownRefresh();
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (!this.data.isLoadedAll) {
      this.data.current_page++;
      this.getStoreList();
    }
  },

  tabClick: function (e) {
    console.log(e);
    this.setData({
      sliderOffset: e.currentTarget.offsetLeft,
      activeIndex: e.currentTarget.id
    });
  },
  goToDetails: function (e) {
    console.log(e)
    wx.navigateTo({
      url: '../storeDetails/storeDetails?sotreId=' + e.currentTarget.dataset.id,
    })
  }
})